package com.aps.common.boot;

import com.aps.common.code.DistributedSnowflakeIdGenerator;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import static com.aps.common.code.DistributedSnowflakeIdGenerator.MACHINE_ID;


/**
 * @Author nbZhuozh
 * @Date 2025-03-19  14:13
 **/
@Component
public class CoreApplicationAware implements ApplicationContextAware {

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        CoreApplicationContext.setApplicationContext(applicationContext);
        //加载雪花算法
        snowflake(applicationContext);
        Thread thread = new Thread(() -> {
            int i = 10000 ;
            for (int j = 0; j <=i; j++) {
                System.out.println(DistributedSnowflakeIdGenerator.nextId() + " ====================== thread 1");
            }
        });
        Thread thread1 = new Thread(() -> {
            int i = 10000 ;
            for (int j = 0; j <=i; j++) {
                System.out.println(DistributedSnowflakeIdGenerator.nextId() +" ====================== thread 2");
            }
        });
        Thread thread2 = new Thread(() -> {
            int i = 10000 ;
            for (int j = 0; j <=i; j++) {
                System.out.println(DistributedSnowflakeIdGenerator.nextId() +" ====================== thread 3");
            }
        });
        Thread thread4 = new Thread(() -> {
            int i = 10000 ;
            for (int j = 0; j <=i; j++) {
                System.out.println(DistributedSnowflakeIdGenerator.nextId() +" ====================== thread 4");
            }
        });
        Thread thread5 = new Thread(() -> {
            int i = 10000 ;
            for (int j = 0; j <=i; j++) {
                System.out.println(DistributedSnowflakeIdGenerator.nextId() +" ====================== thread 5");
            }
        });
        thread.start();
        thread1.start();
        thread2.start();
        thread4.start();
        thread5.start();
        //测试生成雪花算法
        System.out.println(DistributedSnowflakeIdGenerator.generate20DigitId());
        System.out.println(DistributedSnowflakeIdGenerator.nextId());
        System.out.println(DistributedSnowflakeIdGenerator.nextId());
        System.out.println(DistributedSnowflakeIdGenerator.nextId());
        System.out.println(DistributedSnowflakeIdGenerator.nextId());
        System.out.println(DistributedSnowflakeIdGenerator.nextId());
        System.out.println(DistributedSnowflakeIdGenerator.nextId());
        System.out.println(DistributedSnowflakeIdGenerator.nextId());
        System.out.println(DistributedSnowflakeIdGenerator.nextId());
        System.out.println("\n" +
                "  _____     ___   ______ _____\n" +
                " |_   _|    | |  |  _   |  _  | \n" +
                "   | |   _ _| |  | | |  | | | | \n" +
                "   | |  | | | |  | | |  | | | | \n" +
                "  _| |_ | |_| |  | |_|  | |_| | \n" +
                " |_____||_____|  |______|_____| \n" +
                " :: IDOO - APS ::                     (v1.0.0)\n" +
                "                                 \n" +
                " [INFO] No active profile set, using default 'dev'\n" +
                " [INFO] Started IdooApplication in 1.8 seconds (JVM running for 2.3)\n"
        );
    }

    //加载雪花算法配置
    private void snowflake(ApplicationContext applicationContext){
        // 从环境变量获取机器号（默认1，需外部配置如SNOWFLAKE_MACHINE_ID=123）
        String machineIdStr = applicationContext.getEnvironment().getProperty("app.snowflake.machine-id");
        long machineId = 1L;
        if (machineIdStr != null && !machineIdStr.isEmpty()) {
            machineId = Long.parseLong(machineIdStr);
        }
        // 校验机器号范围（1-999 → 对应3位十进制）
        if (machineId < 1 || machineId > 127) {
            throw new IllegalArgumentException("机器号必须为1-127的整数");
        }
        // 校验机器号是否在10位二进制范围内（防止溢出）
        if ((machineId & DistributedSnowflakeIdGenerator.MACHINE_ID_MASK) != machineId) {
            throw new IllegalArgumentException("机器号超出10位二进制范围（最大1023）");
        }
        MACHINE_ID = machineId;
    }
}
